package lt.marsrutai.mobile.android.adapter;

import java.util.List;
import java.util.Map;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.model.ArrivalTime;
import lt.marsrutai.mobile.android.model.Schedule;
import lt.marsrutai.mobile.android.model.Stop;
import lt.marsrutai.mobile.android.model.StopBookmark;
import lt.marsrutai.mobile.android.model.enums.TransportType;
import lt.marsrutai.mobile.android.util.NextDepartures;
import lt.marsrutai.mobile.android.util.TimeUntilNextDeparture;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class StopBookmarkAdapter extends ListViewAdapter<StopBookmark> {

    private Map<StopBookmark, Stop> stops;
    private Map<StopBookmark, Schedule> schedules;
    private Map<StopBookmark, List<ArrivalTime>> stopTimes;

    public StopBookmarkAdapter(Context context, Map<StopBookmark, Stop> stops,  Map<StopBookmark, Schedule> schedules, Map<StopBookmark, List<ArrivalTime>> stopTimes) {
        super(context, R.layout.stop_bookmark_item);

        this.stops = stops;
        this.schedules = schedules;
        this.stopTimes = stopTimes;
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {

        StopBookmark stopBookmark = getItem(position);
        Stop stop = stops.get(stopBookmark);
        Schedule schedule = schedules.get(stopBookmark);
        List<ArrivalTime> trackStopTimes = stopTimes.get(stopBookmark);

        if (stopBookmark != null) {
            TextView scheduleNumber = (TextView) view.findViewById(R.id.schedule_number);
            if (scheduleNumber != null) {
                if (!TransportType.TRAIN.equals(schedule.getTransportType())) {
                    scheduleNumber.setText(schedule.getName());
                }
                scheduleNumber.setBackgroundColor(AppContext.getTransportInfoResolver().resolveTransportColor(stopBookmark.getScope(), schedule.getTransportId()));
            }

            TextView stopName = (TextView) view.findViewById(R.id.stop_name);
            if (stopName != null) {
                stopName.setText(stop.getName() + ", " + stop.getDirectionName());
            }

            TextView nextStopTimes = (TextView) view.findViewById(R.id.next_stop_times);
            if (nextStopTimes != null) {
                nextStopTimes.setText(new NextDepartures(trackStopTimes).toString());
            }

            TextView timeRemaining = (TextView) view.findViewById(R.id.time_remaining);
            if (timeRemaining != null) {
                timeRemaining.setText(new TimeUntilNextDeparture(trackStopTimes).toString());
            }
        }
        return view;
    }
}
